﻿Imports System.IO
Imports System.Text.RegularExpressions
Imports System.String

Public Class Form1
    Dim ARPEZZO As New ArrayList
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        OpenFileDialog1.ShowDialog()
        Dim file As New StreamReader(OpenFileDialog1.FileName)
        Dim RIGA As String
        Dim cambiutensile As Integer
        Dim numerocolpi As Integer
        Dim spn = ("(N\d+)[\s\S](X\d+\.\d+)[\s\S](Y\d+\.\d+)[\s\S](SPN=)(\d+)")
        Dim dc = ("(N\d+)[\s\S](X\d+\.\d+)[\s\S](Y\d+\.\d+)[\s\S](C\d+\=DC\(\d+\.\d+\))")
        Dim colpo = ("(N\d+)[\s\S](X\d+\.\d+)[\s\S](Y\d+\.\d+)$")
        Dim entrypunch = ("(;ENTRY_PUNCH:)")
        Dim UT = ("(N\d+)[\s\S]MSG\(.UT\.")
        Dim PEZZO As String = ""
        Dim PTT As New ArrayList
        cambiutensile = 0
        numerocolpi = 0

        While Not file.EndOfStream
            RIGA = file.ReadLine

            If Regex.IsMatch(RIGA, UT) Then
                ListBox1.Items.Add(RIGA)
            End If

            If RIGA.Contains("ENTRY_PUNCH") Then
                ARPEZZO.Add(PEZZO)
                PEZZO = ""
                cambiutensile += 1
            End If

            If RIGA.Contains("ENDE_PROGRAMM") Then
                ARPEZZO.Add(PEZZO)
                PEZZO = ""
            End If

            If Regex.IsMatch(RIGA, spn) Then
                'ListBox1.Items.Add(RIGA)
                For Each Group As Match In Regex.Matches(RIGA, spn)
                    ListBox3.Items.Add(Group.Groups(5))
                    numerocolpi += Group.Groups(5).Value
                Next
                PEZZO += (RIGA & vbCrLf)
            End If

            If Regex.IsMatch(RIGA, dc) Then
                'ListBox1.Items.Add(RIGA)
                PEZZO += (RIGA & vbCrLf)
                numerocolpi += 1
            End If

            If Regex.IsMatch(RIGA, colpo) Then
                ' ListBox1.Items.Add(RIGA)
                PEZZO += (RIGA & vbCrLf)
                numerocolpi += 1
            End If

            ListBox2.Items.Add(RIGA)

        End While

        Dim tempocolpi As Int16
        tempocolpi = numerocolpi * 0.2
        Label2.Text = numerocolpi
        Label4.Text = (tempocolpi) / 60
        ' TextBox1.Text = ARPEZZO(1)

    End Sub


    
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        ' Dim numerolistbox1 As Integer
        'ListBox1.Items.Count
        TextBox1.Text = ""
        TextBox1.Text = ARPEZZO(ListBox1.SelectedIndex + 1)
    End Sub

   

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label2.Text = ""
        Label4.Text = ""
    End Sub
End Class

